package cn.edu.scujj.dao;

import cn.edu.scujj.bean.PurcharTongJi;
import cn.edu.scujj.bean.TongJi;
import cn.edu.scujj.utils.DButil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

public class PurcharTongJiDao {

    // 添加商品采购记录
    public boolean addPurcharTongji(PurcharTongJi tj) {

        String sql = "INSERT INTO purchartongji (name, num, jinjia, chushoujia) VALUES (?, ?, ?, ?)";
        try (Connection conn = DButil.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {

            pstmt.setString(1, tj.getName());
            pstmt.setDouble(2, tj.getNum());
            pstmt.setFloat(3, tj.getJinjia());
            pstmt.setFloat(4, tj.getChushoujia());
            int rows = pstmt.executeUpdate();
            return rows > 0;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    // 更新商品采购记录
    public boolean updatePurcharTongji(PurcharTongJi tj) {
        String sql = "UPDATE purchartongji SET num = ?, jinjia = ?, chushoujia = ? WHERE name = ?";
        try (Connection conn = DButil.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {

            pstmt.setDouble(1, tj.getNum());
            pstmt.setFloat(2, tj.getJinjia());
            pstmt.setFloat(3, tj.getChushoujia());
            pstmt.setString(4, tj.getName());

            int rows = pstmt.executeUpdate();
            return rows > 0;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    // 删除商品采购记录
    public boolean deletePurcharTongji(String name) {
        String sql = "DELETE FROM purchartongji WHERE name = ?";
        try (Connection conn = DButil.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {

            pstmt.setString(1, name);

            int rows = pstmt.executeUpdate();
            return rows > 0;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    // 查询所有商品采购记录
    public List<PurcharTongJi> findAll() {
        List<PurcharTongJi> list = new ArrayList<>();
        String sql = "SELECT name, num, jinjia, chushoujia FROM purchartongji";
        try (Connection conn = DButil.getConnection();
             Statement stmt = conn.createStatement();
             ResultSet rs = stmt.executeQuery(sql)) {

            while (rs.next()) {
                PurcharTongJi tj = new PurcharTongJi();
                tj.setName(rs.getString("name"));
                tj.setNum(rs.getDouble("num"));
                tj.setJinjia(rs.getFloat("jinjia"));
                tj.setChushoujia(rs.getFloat("chushoujia"));
                list.add(tj);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }
}
